package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;

public class Register extends AppCompatActivity {

    private TextInputEditText etName, etPassword, etConfirmPassword;
    private MaterialButton btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout); // 替换为你的布局文件名
        // 初始化视图
        etName = findViewById(R.id.etName);
        etPassword = findViewById(R.id.etPassword);
        etConfirmPassword = findViewById(R.id.etConfirmPassword);
        btnRegister = findViewById(R.id.btnRegister);
        // 注册按钮点击事件
        btnRegister.setOnClickListener(v -> attemptRegister());
    }

    private void attemptRegister() {
        // 重置错误状态
        etName.setError(null);
        etPassword.setError(null);
        etConfirmPassword.setError(null);
        // 获取输入值
        String username = etName.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String confirmPassword = etConfirmPassword.getText().toString().trim();
        // 验证输入
        boolean cancel = false;
        View focusView = null;
        if (TextUtils.isEmpty(username)) {
            etName.setError("请输入账号");
            focusView = etName;
            cancel = true;
        } else if (TextUtils.isEmpty(password)) {
            etPassword.setError("请输入密码");
            focusView = etPassword;
            cancel = true;
        } else if (password.length() < 6) {
            etPassword.setError("密码至少6位");
            focusView = etPassword;
            cancel = true;
        } else if (!password.equals(confirmPassword)) {
            etConfirmPassword.setError("密码不一致");
            focusView = etConfirmPassword;
            cancel = true;
        }
        if (cancel) {
            focusView.requestFocus();
        } else {
            // 执行注册逻辑
            saveCredentials(this, username, password);
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();

            // 返回登录界面
            Intent intent = new Intent(Register.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
        }
    }
    // 存储账号密码到SharedPreferences
    private void saveCredentials(Context context, String username, String password) {
        SharedPreferences sharedPref = context.getSharedPreferences("accountpasswd", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("username", username);
        editor.putString("password", password); // 注意：实际开发中密码需加密存储！
        editor.apply();
    }
}
